package cn.webrx.bs.servlet;

import cn.webrx.bs.db.DbUtil;
import cn.webrx.bs.db.DruidUtil;
import cn.webrx.bs.service.AdminLogDao;
import cn.webrx.bs.service.AdminLogImpl;
import cn.webrx.bs.utils.IPUtil;
import com.wf.captcha.utils.CaptchaUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-08-05 11:22
 */
@WebServlet("/admin/admin")
public class Admin extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String a = req.getParameter("action");
        if ("add".equals(a)) {
            req.getRequestDispatcher("/admin/adminadd.ftl").forward(req, resp);
        } else if ("login".equals(a)) {
            String msg = "登录失败";
            boolean flag = false;
            if (CaptchaUtil.ver(req.getParameter("checks"), req)) {
                CaptchaUtil.clear(req);  // 清除session中的验证码
                String aa = req.getParameter("account");//账号 手机号
                String pp = req.getParameter("passwd");
                QueryRunner run = new QueryRunner(DruidUtil.getDataSource());
                try {
                    Map<String, Object> map = run.query("select id,account,passwd,truename,tel from bs_admin where account = ? or tel = ?", new MapHandler(), aa, aa);
                    if (map != null) {
                        DbUtil du = new DbUtil();
                        if (du.isLogin(map.get("account").toString(), pp, map.get("tel").toString(), map.get("passwd").toString())) {
                            msg = "登录成功";
                            HttpSession session = req.getSession();
                            session.setAttribute("admin",map.get("account"));
                            session.setAttribute("truename",map.get("truename"));
                            AdminLogDao log = new AdminLogImpl();
                            log.add("登录",new Date(), IPUtil.ipToLong(req.getRemoteAddr().equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : req.getRemoteAddr()),map.get("account").toString());
                            flag = true;
                        }
                    }else{
                        msg = msg + ":账号密码错误";
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }else{
                msg = msg + ":验证码错误";
            }
            req.setAttribute("msg", msg);
            if(flag){
                resp.sendRedirect("/admin");
            }else {
                req.getRequestDispatcher("/admin/admininfo.ftl").forward(req, resp);
            }
        } else if ("save".equals(a)) {
            //1账号手机号必须填写，不重复 2 两次密码一致

            String aa = req.getParameter("account");
            String pp = req.getParameter("pwd");
            String tt = req.getParameter("tel");
            String nn = req.getParameter("truename");

            QueryRunner run = new QueryRunner(DruidUtil.getDataSource());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String myt = sdf.format(new Date());
            String pass = new DbUtil().mm(aa + pp + tt);
            try {
                run.execute("insert into bs_admin values(null,?,?,?,?,?,?,?)", aa, pass, nn, tt, myt, myt, myt);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            req.setAttribute("msg", "账号建立成功");
            req.getRequestDispatcher("/admin/admininfo.ftl").forward(req, resp);
        }else {
            req.getRequestDispatcher("/admin/login.ftl").forward(req, resp);
        }
    }
}
